type CurThemes='light'|'dark';
type Themes=CurThemes|'auto';
let nowOSTheme:CurThemes='light';
const prefers=window.matchMedia('(prefers-color-scheme: dark)');
if (prefers.matches){
    nowOSTheme='dark';
}
prefers.addEventListener("change", function (event) {
    nowOSTheme = event.matches ? 'dark' : 'light';
    if (nowTheme==='auto'){
        nowCurTheme=nowOSTheme;
        refreshCurTheme();
    }
})
let nowTheme:Themes='auto';
let nowCurTheme:CurThemes=nowOSTheme;
export const themes:readonly Themes[]=Object.freeze(['light', 'dark', 'auto']);
const curThemeCallbacks:((theme:CurThemes)=>void)[]=[];
export function getCurTheme(callback:(theme:CurThemes)=>void){
    curThemeCallbacks.push(callback);
    callback(nowCurTheme);
}
const themeCallbacks:((theme:Themes)=>void)[]=[];
export function getTheme(callback:(theme:Themes)=>void){
    themeCallbacks.push(callback);
    callback(nowTheme);
}
function refreshCurTheme(){
    curThemeCallbacks.forEach(callback=>callback(nowCurTheme));
}
function refreshTheme(){
    themeCallbacks.forEach(callback=>callback(nowTheme));
}
export function setTheme(theme:Themes){
    nowTheme = theme;
    if(nowTheme==='auto'){
        nowCurTheme=nowOSTheme;
    }
    else{
        nowCurTheme=nowTheme;
    }
    refreshCurTheme();
    refreshTheme();
}